import PageConst from './PageConst';
import Sgxk from './../../components/sgxk';
import Ht from './../../components/ht';
import Xm from './../../components/xm';
import Zb from './../../components/zb';
import Ts from './../../components/ts';
import Camera from "../../components/camera";

export default {
  defaults(props) {
    return {
      dialogTitle: '',
      showDialog: false,
      dialogWidth: 1050,
      showDialogLx: '',
      ...PageConst,
      steps: [],
    }
  },

  //显示明细弹窗
  showModal(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        showDialog: {$set: true},
      })
    );
  },

  //隐藏明细弹窗
  hideModal(ctx) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        showDialog: {$set: false},
      })
    );
  },

  initColoums(ctx, sgxkColoums, zbgcColoums, htColoums, xmColoums, tsColoums, kqjlColoums, cameraColoums){
    ctx.setState(
      React.addons.update(ctx.getState(), {
        sgxkProps: {
          jsxcolumns: {
            $set: sgxkColoums
          }
        },
        bdProps: {
          jsxcolumns: {
            $set: zbgcColoums
          }
        },
        htProps: {
          jsxcolumns: {
            $set: htColoums
          }
        },
        xmProps: {
          jsxcolumns: {
            $set: xmColoums
          }
        },
        tsProps: {
          jsxcolumns: {
            $set: tsColoums
          }
        },
        kqjlProps: {
          jsxcolumns: {
            $set: kqjlColoums
          }
        },
        cameraTable: {
          jsxcolumns: {
            $set: cameraColoums
          }
        }
      })
    );
  },

  // 监督工程基本信息查询
  async jdgc(ctx, jdgc) {


    const content = await ctx.fn.DB.xmmx.info.jdgc({Authorization: ctx.getProps().token, ...jdgc}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          jdgc: {$set: content},
        })
      );
    }

    return {
      ...jdgc,
      sgxkId: content ? content.sgxkId : null,
    }

  },


  // 违规处理查询
  async error(ctx, jdgc, sgxk) {

    const content = await ctx.fn.DB.xmmx.info.error({Authorization: ctx.getProps().token, ...jdgc}).catch(
      (error) => {
        console.log(error);
      }
    );

    if (content) {
      let wg = {
        xAxis: [],
        series: []
      };
      content.map((kv)=> {
        wg.xAxis.push(kv.text);
        wg.series.push(kv.value);
      });

      ctx.setState({
        wgOption: React.addons.update(ctx.getState().wgOption, {
          xAxis: {
            $apply: (xAxis) => {
              return xAxis.map(item=> {
                return React.addons.update(item, {data: {$set: wg.xAxis}})
              })
            }
          },
          series: {
            $apply: (series) => {
              return series.map(item=> {
                return React.addons.update(item, {data: {$set: wg.series}})
              })
            }
          }
        })
      });
    }

    return {
      ...sgxk,
    };
  },


  // 施工许可信息查询
  async sgxk(ctx, jdgc, sgxk) {

    const content = await ctx.fn.DB.xmmx.info.sgxkgc({Authorization: ctx.getProps().token, ...sgxk}).catch(
      (error) => {
        console.log(error);
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          sgxkProps: {
            jsxdata: {
              data: {$set: content}
            }
          }
        })
      );
    }

    return {
      ...sgxk,
      sgzbId: content.length > 0 ? content[0].sgzbid : '',
      jlzbId: content.length > 0 ? content[0].jlzbid : '',
      sgzbName: content.length > 0 ? content[0].name : '',
      jlzbName: content.length > 0 ? content[0].name : '',
    };
  },

  // 招标工程信息查询
  async zbgc(ctx, sgxk, zbgc) {

    const content = await ctx.fn.DB.xmmx.info.zbgc({Authorization: ctx.getProps().token, ...zbgc}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          bdProps: {
            jsxdata: {
              data: {$set: content}
            }
          }
        })
      )
    }

    return {
      ...zbgc,
      xmId: content.length > 0 ? content[0].xmId : '',
    };
  },
  // 合同信息查询
  async ht(ctx, sgxk, zbgc) {

    const content = await ctx.fn.DB.xmmx.info.ht({Authorization: ctx.getProps().token, ...zbgc}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          htProps: {
            jsxdata: {
              data: {$set: content}
            }
          }
        })
      )
    }

    return {
      ...zbgc,
      htId: content.length > 0 ? content[0].id : '',
    };
  },
  // 图审信息查询
  async ts(ctx, sgxk, zbgc) {

    const content = await ctx.fn.DB.xmmx.info.ts({Authorization: ctx.getProps().token, ...zbgc}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          tsProps: {
            jsxdata: {
              data: {$set: content}
            }
          }
        })
      )
    }

    return {
      ...zbgc,
      tsId: content.length > 0 ? content[0].id : '',
    };
  },

  // 项目信息查询
  async xm(ctx, sgxk, zbgc) {

    const content = await ctx.fn.DB.xmmx.info.xm({Authorization: ctx.getProps().token, ...zbgc}).catch(
      (error) => {
        console.log(error);
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          xmProps: {
            jsxdata: {
              data: {$set: content}
            }
          }
        })
      )
    }

    return {
      ...zbgc,
      xmId: content.length > 0 ? content[0].id : ''
    }
  },

  async timeline(ctx, sgxk, params){

    const content = await
      ctx.fn.DB.xmmx.info.timeline({Authorization: ctx.getProps().token, ...params}).catch(
        (error) => {
          console.log(error);
        }
      );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          steps: {
            $set: content
          },
        }));
    }

  },

  async viewMx(ctx, data, lx){
    switch (lx) {
      case  'sgxk':
        const ghxj = await
          ctx.fn.DB.xmmx.info.ghxk({
              sgxkId: data.id,
              Authorization: ctx.getProps().token
            }
          ).catch(
            (error) => {
              console.log(error);
            }
          );
        const sgxmb = await
          ctx.fn.DB.xmmx.info.sgxmb({
            sgxkId: data.id,
            Authorization: ctx.getProps().token
          }).catch(
            (error) => {
              console.log(error);
            }
          );

        const jljg = await
          ctx.fn.DB.xmmx.info.jljg({
            sgxkId: data.id,
            Authorization: ctx.getProps().token
          }).catch(
            (error) => {
              console.log(error);
            }
          );
        ctx.setState(
          React.addons.update(ctx.getState(), {
            dialogTitle: {$set: '施工许可明细'},
            dialogWidth: {$set: 1050},
            showDialogLx: {
              $set: <Sgxk data={data} ghxk={ghxj} sgxmb={sgxmb} docUrl={data.docUrl} jljg={jljg} token={ctx.getProps().token}/>
            },
          }));
        break;
      case  'zbgc':

        const dlry = await
          ctx.fn.DB.xmmx.info.zbdlry(
            {
              xmId: data.id,
              Authorization: ctx.getProps().token
            }
          ).catch(
            (error) => {
              console.log(error);
            }
          );

        const zbry = await
          ctx.fn.DB.xmmx.info.zbjgry({
            xmId: data.id,
            Authorization: ctx.getProps().token
          }).catch(
            (error) => {
              console.log(error);
            }
          );

        ctx.setState(
          React.addons.update(ctx.getState(), {
            dialogTitle: {$set: '招标明细'},
            dialogWidth: {$set: 1050},
            showDialogLx: {
              $set: <Zb data={data} dlry={dlry} zbry={zbry} token={ctx.getProps().token}/>
            },
          }));

        break;
      case  'ht':

        const htdxgc = await
          ctx.fn.DB.xmmx.info.htdxgc({
            htId: data.id,
            Authorization: ctx.getProps().token
          }).catch(
            (error) => {
              console.log(error);
            }
          );

        ctx.setState(
          React.addons.update(ctx.getState(), {
            dialogTitle: {$set: '合同明细'},
            dialogWidth: {$set: 1050},
            showDialogLx: {
              $set: <Ht data={data} dxgc={htdxgc} token={ctx.getProps().token}/>
            },
          }));
        break;
      case  'xm':

        const zjly = await
          ctx.fn.DB.xmmx.info.xmzjly({
            xmId: data.id,
            Authorization: ctx.getProps().token
          }).catch(
            (error) => {
              console.log(error);
            }
          );

        const lxpw = await
          ctx.fn.DB.xmmx.info.xmlxpw({
            xmId: data.id,
            Authorization: ctx.getProps().token
          }).catch(
            (error) => {
              console.log(error);
            }
          );

        ctx.setState(
          React.addons.update(ctx.getState(), {
            dialogTitle: {$set: '项目明细'},
            dialogWidth: {$set: 1050},
            showDialogLx: {
              $set: <Xm data={data} zjly={zjly} lxpw={lxpw} token={ctx.getProps().token}/>
            },
          }));
        break;
      case  'ts':

        ctx.setState(
          React.addons.update(ctx.getState(), {
            dialogTitle: {$set: '图审明细'},
            dialogWidth: {$set: 1050},
            showDialogLx: {
              $set: <Ts data={data} token={ctx.getProps().token}/>
            },
          }));

        break;
      case 'camera':
        ctx.setState(
          React.addons.update(ctx.getState(), {
            dialogTitle: {$set: '工地视频'},
            dialogWidth: {$set: 660},
            showDialogLx: {
              $set: <Camera data={data} token={ctx.getProps().token}/>
            },
          }));
        break;
      default:
        break;
    }
  },

  async kqjl(ctx, props) {
    const content = await ctx.fn.DB.xmmx.info.kqjl({Authorization: ctx.getProps().token, ...props}).catch(
        (error) => {
          console.log(error);
        }
      );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          kqjlProps: {
            jsxdata: {
              data: {$set: content}
            }
          },
        }));
    }
  },

  async spjk(ctx, props) {

    const content = await ctx.fn.DB.xmmx.info.cameraInfo({Authorization: ctx.getProps().token, ...props}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content && content.length > 0) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          cameraTable: {
            jsxdata: {
              data: {$set: content}
            }
          }
        }));
    }

  },

}
